﻿
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using EntitiesLayer;
using System.Transactions;
using System.Data.SqlClient;
using System.Data;
using DAL.Repositories;
using DAL.AbstractRepositories;

namespace DAL.Repositorios
{
    /// <summary>
    /// Repositorio para los GRUDS de los correos de usuario
    /// (100%Probado)
    /// </summary>
    public class CorreoElectronicoRepository : DAL.AbstractRepositories.AbstractRepository<CorreoElectronico>
    {

        

        public override void Insert(CorreoElectronico entity)
        {
            Inserted.Add(entity);
        }

        public override void Delete(CorreoElectronico entity)
        {
            Deleted.Add(entity);
        }

        public override void Update(CorreoElectronico entity)
        {
            Updated.Add(entity);
        }

        public override IEnumerable<CorreoElectronico> GetAll()
        {
            SqlCommand cmd = new SqlCommand();
            DataSet ds = DBAccess.ExecuteSPWithDS(ref cmd, "PaListarCorreo ");

            foreach (DataRow dr in ds.Tables[0].Rows)
                yield return crearCorreo(Convert.ToInt32(dr["IdCorreo"]), dr);


        }

        public override CorreoElectronico GetById(int id)
        {
            CorreoElectronico correo = null;
            SqlCommand cmd = new SqlCommand();
            cmd.Parameters.Add(new SqlParameter("@IdCorreo", id));
            DataSet ds = DBAccess.ExecuteSPWithDS(ref cmd, "PaConsultarCorreoPorID ");

            foreach (DataRow dr in ds.Tables[0].Rows)
                correo = crearCorreo(id, dr);



            return correo;
        }


        protected override void DeleteEntity(CorreoElectronico c)
        { 
          
                SqlCommand cmd = new SqlCommand();
                cmd.Parameters.Add(new SqlParameter("@IdCorreo", c.IdUsuario));
                DBAccess.ExecuteSPNonQuery(ref cmd, "PaEliminarCorreo");
        
        }

        protected override void InsertEntity(CorreoElectronico c) {
           
                SqlCommand cmd = new SqlCommand();
                cmd.Parameters.Add(new SqlParameter("@IdUsuario", c.IdUsuario));
                cmd.Parameters.Add(new SqlParameter("@CorreoElectronico", c.Correo));
                cmd.Parameters.Add(new SqlParameter("@DescTipoCorreo", c.Tipo));

                DBAccess.ExecuteSPNonQuery(ref cmd, "PaRegistrarCorreo");
           
        }

        protected override void UpdateEntity(CorreoElectronico c)
        {

                SqlCommand cmd = new SqlCommand();
                cmd.Parameters.Add(new SqlParameter("@IdCorreo", c.Id));
                cmd.Parameters.Add(new SqlParameter("@IdUsuario", c.IdUsuario));
                cmd.Parameters.Add(new SqlParameter("@CorreoElectronico", c.Correo));
                cmd.Parameters.Add(new SqlParameter("@DescTipoCorreo", c.Tipo));

                DataSet ds = DBAccess.ExecuteSPWithDS(ref cmd, "PaModificarCorreo");            
        }


        internal static IList<CorreoElectronico> correosDeUsuario(int idUsuario){

            IList<CorreoElectronico> listacorreo =  new List<CorreoElectronico>();
            SqlCommand cmd = new SqlCommand();
            cmd.Parameters.Add(new SqlParameter("@IdUsuario", idUsuario));
            DataSet ds = DBAccess.ExecuteSPWithDS(ref cmd, "PaConsultarCorreosUsuario");

            foreach (DataRow dr in ds.Tables[0].Rows)
            {
                   
                    listacorreo.Add(
                       crearCorreo(Convert.ToInt32(dr["IdCorreo"]), dr)
                    );
            }

            return listacorreo.Count>0?listacorreo:null;
        }

       // internal Notificacion crearNotificacion(int id, DataRow dr) {
         //   throw new NotImplementedException();
        //}


        internal static CorreoElectronico crearCorreo(int id, DataRow dr) {

            return new CorreoElectronico(
                idCorreo: id,
                correo: dr["CorreoElectronico"].ToString(),
                tipo: dr["DescTipoCorreo"].ToString(),
                idUsuario: Convert.ToInt32(dr["IdUsuario"])
                
                );
        
        }
    }
}
